export const useEnumStore = defineStore('enums', () => {
  const enums = ref<
    Array<{
      key: string
      value: EnumDataOption[]
    }>
  >([])

  /**
   * 获取字典
   * @param _key 字典key
   */
  const getEnum = (_key: string): EnumDataOption[] | null => {
    if (_key == null && _key == '') {
      return null
    }
    try {
      for (let i = 0; i < enums.value.length; i++) {
        if (enums.value[i].key == _key) {
          return enums.value[i].value
        }
      }
    } catch (e) {
      return null
    }
    return null
  }

  /**
   * 设置字典
   * @param _key 字典key
   * @param _value 字典value
   */
  const setEnum = (_key: string, _value: EnumDataOption[]) => {
    if (_key !== null && _key !== '') {
      enums.value.push({
        key: _key,
        value: _value,
      })
    }
  }

  /**
   * 删除字典
   * @param _key
   */
  const removeEnum = (_key: string): boolean => {
    let bln = false
    try {
      for (let i = 0; i < enums.value.length; i++) {
        if (enums.value[i].key == _key) {
          enums.value.splice(i, 1)
          return true
        }
      }
    } catch (e) {
      bln = false
    }
    return bln
  }

  /**
   * 清空字典
   */
  const cleanEnum = (): void => {
    enums.value = []
  }

  return {
    enums,
    getEnum,
    setEnum,
    removeEnum,
    cleanEnum,
  }
})

export default useEnumStore
